import { _decorator, Button, Label, tween, Tween, UITransform, v3, Widget } from 'cc';
import { Ab_BasePopUp_bA } from '../../../A-FRAME/component/ui.pop-up';
import Ab_Config_bA from '../../manager/config.manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
import { Ab_Float_bA } from '../page/float';
import type { Ab_Popup_bA } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('Ab_Free_bA')
export class Ab_Free_bA extends Ab_BasePopUp_bA<typeof Ab_Popup_bA, null> {
	protected Ab_default_return_bA: null;

	private Ab_uitrans_bA: UITransform;
	private Ab_ui_container_bA: UITransform;
	private Ab_ui_real_item_bA: UITransform;
	private Ab_lbl_symbol_bA: Label;
	private Ab_item_step_bA = 110;

	protected Ab_initAfterOnLoad_bA(){
		this.Ab_uitrans_bA = this.node.getComponent(UITransform);
		this.Ab_ui_container_bA = this.node.getChildByName('container').getComponent(UITransform);
		this.Ab_ui_real_item_bA = this.Ab___btn_map___bA.get('close').node.parent.getComponent(UITransform);
		this.Ab_lbl_symbol_bA = this.Ab_ui_real_item_bA.node.getChildByName('lbl-symbol').getComponent(Label);

		this.Ab_renderLocalUI_bA();
	}

	protected Ab__click_event__bA: { [name: string]: (this: Ab_Free_bA, button: Button) => void; } = {
		close(btn){
			btn.interactable = false;
			this.Ab_leave_bA();
		},
		free(btn){
			btn.interactable = false;
			Ab_Float_bA.Ab_Hint_bA(Ab_GameAssets_bA.Ab_getLangText_bA(4))
			.then(()=>btn.interactable = true);
		}
	}

	public Ab_onShow_bA(){
		this.Ab_refresh_bA(Ab_GameDC_bA.Ab_User_bA.Ab_round_bA);
		this.Ab_enter_bA();
	}

	private Ab_renderLocalUI_bA(){
		this.Ab___btn_map___bA.get('free').node.getChildByName('label').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(5);
		this.Ab_ui_real_item_bA.node.getChildByName('lbl-desc').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(4);
		this.Ab_ui_real_item_bA.node.getChildByName('lbl-head').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(3);

		abi.Ab_cc_bA.Ab_skinPeeler_bA(this.Ab_ui_real_item_bA.node.getChildByName('icon'), Ab_GameAssets_bA.Ab_getCounTexture_bA(3), {height: true});
	}

	private Ab_refresh_bA(curr_round: number){
		this.Ab_calculate_bA();

		// round: 1; cur_idx: 0; show_item: 3
		// round: 2; cur_idx: 1; show_item: 2 3
		// round: 3; cur_idx: 2; show_item: 1 2 3
		// round: 4; cur_idx: 3; show_item: 0 1 2 3

		// cur_idx: 0; 3: 1
		// cur_idx: 1; 3: 2; 2: 1
		// cur_idx: 2; 3: 3; 2: 2; 1: 1
		// cur_idx: 3; 3: 4; 2: 3; 1: 2; 0: 1

		let max_idx = 3;
		let cur_idx = curr_round-1;
		let top_y = cur_idx * this.Ab_item_step_bA;

		let task_list = Ab_Config_bA.Ab_taskRound_bA;

		this.Ab_ui_container_bA.node.children.forEach((n, i)=>{
			let show = (i+cur_idx) >= max_idx;
			n.active = show;
			if(show){
				n.setPosition(n.position.x, top_y, n.position.z);
				let show_round = (cur_idx+1) - (max_idx - i);
				n.getChildByName('lbl-round').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(0, show_round);
				n.getChildByName('lbl-hard').getComponent(Label).string = task_list.find(el=>el.round==show_round).hard ? Ab_GameAssets_bA.Ab_getLangText_bA(2) : Ab_GameAssets_bA.Ab_getLangText_bA(1);
			}
		});

		this.Ab_lbl_symbol_bA.string = Ab_Config_bA.Ab_symbolString_bA(Ab_GameDC_bA.Ab_User_bA.Ab_symbol_bA);
	}

	private Ab_enter_bA(){
		let time_step = 0.1;
		this.Ab_ui_container_bA.node.children.filter(n=>n.active).forEach((n, i, ls)=>{
			Tween.stopAllByTarget(n);

			tween(n)
			.to(time_step * i, {position: v3(n.position.x, (ls.length-1-i) * this.Ab_item_step_bA, n.position.z)}, {easing: 'bounceOut'})
			.start();
		});
	}

	private Ab_leave_bA(){
		let time_step = 0.1;
		let list = this.Ab_ui_container_bA.node.children.filter(n=>n.active);

		let target_potisition = v3(0, 0, 0);

		let np = new abi.Ab_NudityPromise_bA<'end'>();
		list.forEach((n, i, ls)=>{
			Tween.stopAllByTarget(n);

			tween(n)
			.to(time_step * (ls.length - 1 - i), {position: target_potisition}, {easing: 'cubicOut'})
			.call(()=>i==0 ? np.Ab_resolve_bA('end') : null)
			.start();
		});

		np.Ab_promise_bA.then(()=>{
			this.Ab_close_bA(null);
			this.Ab___btn_map___bA.get('close').interactable = true;
		});
	}

	private Ab_calculate_bA(){
		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();

		let top = this.Ab_uitrans_bA.height * (1-this.Ab_uitrans_bA.anchorY) - this.Ab_ui_container_bA.height * (1-this.Ab_ui_container_bA.anchorY) - this.Ab_ui_container_bA.node.position.y;
		if(abi.Ab_cc_bA.Ab_is_long_screen_bA) top -= 100;
		this.Ab_item_step_bA = Math.min(110, top / 3);
	}
}

